
package com.wulian.user.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.wulian.common.util.Query;
import com.wulian.common.vo.ResultMoudel;
import com.wulian.user.dto.RoleDTO;
import com.wulian.user.entity.SysRole;
import com.wulian.user.entity.SysRoleDept;
import com.wulian.user.entity.SysTree;
import com.wulian.user.mapper.SysRoleDeptMapper;
import com.wulian.user.mapper.SysRoleMapper;
import com.wulian.user.service.SysRoleService;
import com.wulian.user.service.SysTreeService;
import com.wulian.user.service.SysUserRoleService;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Administrator
 * @since 2017-10-29
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
    @Autowired
    private SysRoleDeptMapper sysRoleDeptMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SysTreeService sysTreeService;
    

    /**
     * 添加角色
     *
     * @param roleDto 角色信息
     * @return 成功、失败
     */
    @Override
    public Boolean insertRole(RoleDTO roleDto) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(roleDto, sysRole);
        sysRole.setUpdateTime( new Date(  ) );
        sysRoleMapper.insert(sysRole);
        SysRoleDept roleDept = new SysRoleDept();
        roleDept.setRoleId(sysRole.getRoleId());
        roleDept.setDeptId(roleDto.getRoleDeptId());
        sysRoleDeptMapper.insert(roleDept);
        return true;
    }

    /**
     * 分页查角色列表
     *
     * @param query   查询条件
     * @param wrapper wapper
     * @return page
     */
    @Override
    public Page selectwithDeptPage(Query<Object> query, EntityWrapper<Object> wrapper) {
    	Map<String, Object> params = query.getCondition();
    	String deptId = null == params.get("dept_id") ? "" : params.get("dept_id").toString();
    	SysTree sysTree = new SysTree();
    	sysTree.setTypeId(2);
    	sysTree.setChriledId(Long.valueOf(deptId));
    	//获取当前用户所属部门下所有的部门信息
    	List<SysTree> selectListChannel = sysTreeService.selectListChannel(sysTree);
    	//转换为List<String>
    	List<String> deptIdList = selectListChannel.stream()
        		.map(st -> String.valueOf(st.getChriledId())).collect(Collectors.toList());
    	//为避免deptIdList数据太大转换为字符串,以“,”号隔开,方便下面数据读取
    	String deptIds = deptIdList.stream().collect(Collectors.joining(","));
    	params.put("deptIds", deptIds);
        query.setRecords(sysRoleMapper.selectRolePage(query, params));
        return query;
    }

    /**
     * 更新角色
     *
     * @param roleDto 含有部门信息
     * @return 成功、失败
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean updateRoleById(RoleDTO roleDto) {
        //删除原有的角色部门关系
        SysRoleDept condition = new SysRoleDept();
        condition.setRoleId(roleDto.getRoleId());
        sysRoleDeptMapper.delete(new EntityWrapper<>(condition));

        roleDto.setUpdateTime(new Date());
        //更新角色信息
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(roleDto, sysRole);
        sysRoleMapper.updateById(sysRole);

        //维护角色部门关系
        SysRoleDept roleDept = new SysRoleDept();
        roleDept.setRoleId(sysRole.getRoleId());
        roleDept.setDeptId(roleDto.getRoleDeptId());
        sysRoleDeptMapper.insert(roleDept);
        return true;
    }

    /**
     * 通过部门ID查询角色列表
     *
     * @param
     * @return 角色列表
     */
    @Override
    public List<SysRole> selectListByDeptId() {
        return sysRoleMapper.selectListByDeptId();
    }

	@Override
	public ResultMoudel delRoleById(SysRole sysRole) {
		Map<String,Object> columnMap = new HashMap<String,Object>();
		columnMap.put("role_id", sysRole.getRoleId());
		Integer size = sysUserRoleService.selectByMap(columnMap).size();
		if(size>0) {
			return new ResultMoudel("error", "角色下含有用户，不可删除！");
		}
		sysRoleMapper.updateById(sysRole);
        return new ResultMoudel(ResultMoudel.SUCCESS_FLAG,"角色删除成功");
	}

    @Override
    public List<SysRole> getRoleByCompanyId(SysRole sysRole) {
        EntityWrapper<SysRole>  where = new EntityWrapper();
        where.eq("company_id",sysRole.getCompanyId());
        if(StringUtils.isNotEmpty(sysRole.getRoleName())){
            where.and().like("role_name",sysRole.getRoleName());
        }
        List<SysRole> sysRoles = this.selectList(where);
        return sysRoles;
    }
}
